package com.va.yahooweather;

import com.va.yahooweather.commons.units.TempratureUnit;
import com.va.yahooweather.entity.YReport;
import com.va.yahooweather.net.YahooRetriever;
import com.va.yahooweather.parser.YahooParser;
import org.apache.log4j.Logger;

public class YahooWeatherService {
    private static final Logger log = Logger.getLogger(YahooWeatherService.class);
    
    private YahooRetriever retriever = new YahooRetriever();
    private TempratureUnit unit = TempratureUnit.C;
    
    public YReport getReport(String woeid) {
        YReport report = null;
        
        log.info("Start retrieve weather data for location " + woeid);
        String retFile = retriever.retrieve(woeid, unit);
        if (retFile.equals("")) {
            log.info("Can not retrieve infomation for location " + woeid);
        }
        else {
            log.info("Retrieved data into " + retFile);
            report = new YahooParser().read(retFile);
        }
        return report;
    }

    public TempratureUnit getUnit() {
        return unit;
    }

    public void setUnit(TempratureUnit unit) {
        this.unit = unit;
    }
    
    public void setUnit(String unit) {
        char c = unit.charAt(0);
        if (c == 'c' || c == 'C') {
            log.info("set temprature unit to " + TempratureUnit.C);
            this.unit = TempratureUnit.C;
            return;
        }
        
        log.info("set temprature unit to " + TempratureUnit.F);
        this.unit = TempratureUnit.F;
    }
    
}
